package com.gomyb.redis;

import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * 2019/9/2 15:07
 */
@Service("redisService")
public class RedisServiceImpl implements RedisService {

    @Resource(name = "redisTemplate")
    private RedisTemplate<String, String> redisTemplate;

    @Resource(name = "redisTemplate")
    private RedisTemplate<String, Object> objectRedisTemplate;

    @Override
    public void set(String key, String value, Integer time) {
        redisTemplate.opsForValue().set(key, value);
        redisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    @Override
    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public String hGet(String key, String field) {
        return String.valueOf(redisTemplate.opsForHash().get(key, field));
    }

    @Override
    public void hSet(String key, String field, String value) {
        redisTemplate.opsForHash().put(key, field, value);
    }

    @Override
    public void hDel(String key, String field) {
        redisTemplate.opsForHash().delete(key, field);
    }

    @Override
    public Long incr(String key) {
        return redisTemplate.opsForValue().increment(key);
    }

    @Override
    public void del(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public Set<String> keys(String pattern) {
        return redisTemplate.keys(pattern);
    }

    @Override
    public Set<String> values(String pattern) {
        return redisTemplate.keys(pattern);
    }

    @Override
    public int keySize(String pattern) {
        return redisTemplate.keys(pattern).size();
    }

    @Override
    public void delAll(String pattern) {
        Set<String> keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    @Override
    public <T> T getObj(String key) {
        return (T) objectRedisTemplate.opsForValue().get(key);
    }

    @Override
    public void setObj(String key, Object value) {
        objectRedisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void setObj(String key, Object value, int time) {
        objectRedisTemplate.opsForValue().set(key, value);
        objectRedisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    @Override
    public Set<String> zRange(String key, long start, long end) {
        return redisTemplate.opsForZSet().range(key, start, end);
    }

    @Override
    public <T> List<T> zRangeObj(String key, long start, long end) {
        return (List<T>) objectRedisTemplate.opsForZSet().range(key, start, end);
    }

    @Override
    public Set<String> zRevRange(String key, long start, long end) {
        return redisTemplate.opsForZSet().reverseRange(key, start, end);
    }

    @Override
    public <T> List<T> zRevRangeObj(String key, long start, long end) {
        return (List<T>) objectRedisTemplate.opsForZSet().reverseRange(key, start, end);
    }

    @Override
    public void zAdd(String key, double score, String value) {
        redisTemplate.opsForZSet().add(key, value, score);
    }

    @Override
    public void zRem(String key, String... value) {
        redisTemplate.opsForZSet().remove(key, value);
    }

    @Override
    public void zAddObj(String key, double score, Object value) {
        objectRedisTemplate.opsForZSet().add(key, value, score);
    }

    @Override
    public Long lRem(String key, String value) {
        return redisTemplate.opsForList().remove(key, 0, value);
    }

    @Override
    public Boolean exists(String key) {
        return redisTemplate.countExistingKeys(Arrays.asList(key)) > 0;
    }

    @Override
    public void rPush(String key, String value) {
        redisTemplate.opsForList().rightPush(key, value);
    }

    @Override
    public Long lLen(String key) {
        return redisTemplate.opsForList().size(key);
    }

    @Override
    public String rPop(String key) {
        return redisTemplate.opsForList().leftPop(key);
    }

    @Override
    public String lIndex(String key, Long index) {
        return redisTemplate.opsForList().index(key, index);
    }

    @Override
    public Double zIncrBy(String key, double incr, String value) {
        return redisTemplate.opsForZSet().incrementScore(key, value, incr);
    }

    @Override
    public Double zScore(String key, String value) {
        return redisTemplate.opsForZSet().score(key, value);
    }

    @Override
    public Long zRevRank(String key, String value) {
        return redisTemplate.opsForZSet().reverseRank(key, value);
    }

    @Override
    public Long zCount(String key, Double min, Double max) {
        return redisTemplate.opsForZSet().count(key, min, max);
    }

    @Override
    public Boolean hExists(String key, String field) {
        return null != redisTemplate.opsForHash().get(key, field);
    }

    @Override
    public Long hIncrBy(String key, String field, long value) {
        return redisTemplate.opsForHash().increment(key, field, value);
    }

    @Override
    public void subscribe(MessageListener messageListener, String... channel) {
        byte[][] btChannel = new byte[channel.length][];
        List<byte[]> bytes = Arrays.stream(channel).map(String::getBytes).collect(Collectors.toList());
        for (int i = 0; i < bytes.size(); i++) {
            btChannel[i] = bytes.get(i);
        }
        redisTemplate.getConnectionFactory().getConnection().subscribe(messageListener, btChannel);
    }

    @Override
    public Long publish(String channel, String message) {
        return redisTemplate.getConnectionFactory().getConnection().publish(channel.getBytes(), message.getBytes());
    }

    @Override
    public <T> T execute(SessionCallback<Object> callback) {
        return (T) objectRedisTemplate.execute(callback);
    }

}
